package com.imooc.miaosha.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author simple
 * @version 1.0
 * @date 2018/6/15 22:14
 */
@Slf4j
public class EncodeUtils {
    public static final String SALT = "e'?>`~1|_(";

    public static String md5(String srcStr) {
        if (StringUtils.isBlank(srcStr)) {
            log.error("md5加密的原字符串不能为空");
            return null;
        }
        return DigestUtils.md5Hex(srcStr);
    }

    public static String passMd5(String pass) {
        if (StringUtils.isBlank(pass)) {
            log.error("密码不能为空");
            return null;
        }
        return md5(SALT + pass + SALT);
    }

    public static String passMd5(String pass, String salt) {
        if (StringUtils.isBlank(pass)) {
            log.error("密码不能为空");
            return null;
        }
        if (StringUtils.isBlank(salt)) {
            log.error("盐值不能为空");
            return null;
        }
        return md5(salt + pass + salt);
    }

    public static String passMd5ToDb(String pass, String salt) {
        return passMd5(passMd5(pass), salt);
    }

    public static void main(String[] args) {
        System.out.println(passMd5ToDb("123456", SALT));
    }
}
